package com.yunupmall.controller.ford;

import cn.hutool.core.util.ObjectUtil;
import com.yunupmall.api.fordVO.MembetVo;
import com.yunupmall.entity.admin.memberpojo.Member;
import com.yunupmall.service.ford.service.FordMemberService;
import com.yunupmall.uits.Result;
import lombok.AllArgsConstructor;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpSession;
import java.time.LocalDate;

@Controller
@RequestMapping("/users")
@ResponseBody
@AllArgsConstructor
public class MemberConreonller {

   private final FordMemberService fordMemberService;

   private final Result result;

    /**
     * 注册用户
     * @param membetVo
     * @return
     */
    @PostMapping("/addUsers")
    public Result AddUsers(@RequestBody MembetVo membetVo) {
        try {
             return result.ok(fordMemberService.addMenberUsers(membetVo));
        }catch (Exception e){
             return result.error(e.getMessage());
        }
    }

    /**
     * 用户登录
     * @param phone
     * @param pwd
     * @param session
     * @return
     */
    @PostMapping("/LoginUsers")
    @ResponseBody
    public Result LoginUsers(String phone, String pwd, HttpSession session) {
        try {
            Member member = fordMemberService.LoginUsers(phone, pwd);
            if (ObjectUtil.isNotEmpty(member)) {
                session.setAttribute("member", member);
                return result.ok("成功");
            } else {
                return result.error("没有检索到用户信息");
            }
        }catch (Exception e) {
            return result.error(e.getMessage());
        }
    }
    @GetMapping("/getUsers")
    public Result getUsers(HttpSession session) {
        try {
            Member member = (Member)session.getAttribute("member");
            if (ObjectUtil.isEmpty(member)) {
                return null;
            }else {
                if (ObjectUtil.isNotEmpty(member.getName())) {
                    return result.ok(member.getName());
                }else {
                    return result.ok(member.getPhone());
                }
            }
        }catch (Exception e) {
            return result.error(e.getMessage());
        }
    }
    /**
     * 用户信息修改
     */
    @PostMapping("/update")
    @ResponseBody
    public Result<String> updateData(@RequestBody Member member){
        try {
            return result.ok(fordMemberService.updateDate(member));
        }catch (Exception e) {
            return result.error(e.getMessage());
        }
    }

    /**
     * 用户信息
     */
    @PostMapping("/getList")
    @ResponseBody
    public Result<Member> updateData(){
        try {
            return result.ok(fordMemberService.getList());
        }catch (Exception e) {
            return result.error(e.getMessage());
        }
    }
    /**
     * 用户信息
     */
    @PostMapping("/page")
    @ResponseBody
    public Result<Member> page(@RequestBody MembetVo membetVo){
        try {
            return result.ok(fordMemberService.page(membetVo));
        }catch (Exception e) {
            return result.error(e.getMessage());
        }
    }
}
